package com.randev.snapgallery;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class ImageGalleryAdapter extends BaseAdapter{
	
	Context imageGalleryContext = null;
	List<String> imageNameList = new ArrayList<String>();
	String sdCard = Environment.getExternalStorageDirectory().getAbsolutePath();
	String HOME_DIRECTORY = sdCard + File.separator + "AndroidTest";
	String folderName="";
	
	
	public ImageGalleryAdapter(Context context,String fName, List<String> imageNames) {
		imageGalleryContext = context;
		imageNameList = imageNames;
		folderName = fName;
	}

	@Override
	public int getCount() {
		return imageNameList.size();
	}

	@Override
	public Object getItem(int arg0) {
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		ImageView iv = null;
		
		
			iv = new ImageView(imageGalleryContext);
		    iv.setLayoutParams(new GridView.LayoutParams(150,120));
	
			String imageId = imageNameList.get(arg0);
			try {
				File imageFile = new File(HOME_DIRECTORY + File.separator + folderName + File.separator + imageId);
				
				if (imageFile.exists()) {
	
					try {
						
						Bitmap bitmap = decodeSampledBitmapFromResource(String.valueOf(imageFile), 100, 100);
						Bitmap bmp = Bitmap.createScaledBitmap(bitmap, 100, 100, true);
						
						iv.setImageBitmap(bmp);
					} catch (IllegalArgumentException e) {
						Log.w("Illegal argument exception", e.toString());
					} catch (OutOfMemoryError e) {
						Log.w("Out of memory error :( ", e.toString());
					}
	
				} else {
					Log.w("file doesnt exist", "image does not exist" + imageFile.toString());
				}
			} catch (Exception e) {
				Log.w("Error getting image file", e.toString());
			}

		return iv;
	}

	private int calculateInSampleSize(BitmapFactory.Options options,
			int reqWidth, int reqHeight) {
		// Raw height and width of image
		final int height = options.outHeight;
		final int width = options.outWidth;
		int inSampleSize = 0;

		if (height > reqHeight || width > reqWidth) {
			if (width > height) {
				inSampleSize = Math.round((float) height / (float) reqHeight);
			} else {
				inSampleSize = Math.round((float) width / (float) reqWidth);
			}
		}
		return inSampleSize;
	}

	private  Bitmap decodeSampledBitmapFromResource(String path,
			int reqWidth, int reqHeight) {

		final BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(path, options);

		options.inSampleSize = calculateInSampleSize(options, reqWidth,
				reqHeight);

		options.inJustDecodeBounds = false;
		return BitmapFactory.decodeFile(path, options);
	}

}